<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>告警规则管理</title>
    <jsp:include page="../inc/css.jsp"/>

    <style>
        .batch_table {
            text-align: center;
            width: 100%;
            border: 1px solid #dee2e6;
            border-collapse: collapse;
            border-spacing: 0;
            border-radius: 10px;
        }

        .batch_table th {
            border: 1px solid #dee2e6;
        }

        .batch_table td {
            border: 1px solid #dee2e6;
        }

        .batch_table input {
            width: 100%;
            border: 0;
            outline: none;
            background-color: rgba(0, 0, 0, 0);
            text-align: center;
        }

        .batch_table select {
            width: 100%;
            border: 0;
            outline: none;
            background-color: rgba(0, 0, 0, 0);
            text-align: center;
        }
    </style>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <!-- /.card-header -->
                        <div class="card-header">
                            <h4>${device.company.name} - ${device.name}</h4>

                            <button type="button" title="添加告警规则" class="btn btn-success" onclick="_alarmRuleList.fn.addInfo()">添加告警规则</button>
                            <button type="button" title="批量添加告警规则" class="btn btn-primary" onclick="_alarmRuleList.fn.batchAddInfo()">批量添加告警规则</button>
                            <button type="button" title="批量删除告警规则" class="btn btn-danger" onclick="_alarmRuleList.fn.batchDelModal()">批量删除</button>
                            <button type="button" title="返回列表" class="btn btn-warning" onclick="_alarmRuleList.fn.backList()">返回列表</button>

                            <div class="form-inline">
                                <div class="form-group">
                                    <select id="searchTypeList" class="form-control" style="margin-right: 10px;margin-top: 10px;" onchange="_alarmRuleList.fn.getCodeDictList(0)">
                                        <option value="">全部类别</option>
                                        <option value="2011">实时数据</option>
                                        <option value="2051">分钟数据</option>
                                        <option value="2061">小时数据</option>
                                        <option value="2031">日均数据</option>
                                    </select>
                                </div>
                                <div class="form-group" style="margin-top: 10px;">
                                    <select id="searchCodeDictList" class="form-control">
                                        <option value="">全部参数</option>
                                    </select>
                                    <button type="button" title="查询" class="btn btn-info" style="margin-left: 10px;z-index: 1000;" onclick="_alarmRuleList.fn.reloadData()">查询</button>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA odd" style="width: 3%"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th><input type="checkbox" onclick="$tutu.checkAll(this)" class="checkall tableCheckBox"/></th>
                                        <th style="max-width: 70px;">类别</th>
                                        <th>关联参数</th>
                                        <th style="max-width: 40px;">类型</th>
                                        <th style="max-width: 50px;">蓝线值</th>
                                        <th style="max-width: 50px;">黄线值</th>
                                        <th style="max-width: 50px;">红线值</th>
                                        <th style="max-width: 60px;">单位</th>
                                        <th style="max-width: 70px;">报警类别</th>
                                        <th style="max-width: 70px;">报警时间</th>
                                        <th style="max-width: 160px;">最近触发时间</th>
                                        <th style="width: 80px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>

<div class="modal inModal fade" id="alarmRuleModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">告警规则详情</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <form role="form" class="form-group">
                    <input type="hidden" id="alarmRuleId"/>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">类别：</label>

                        <div class="col-9">
                            <select id="typeList" class="form-control" onchange="_alarmRuleList.fn.getCodeDictList(1)">
                                <option value="2011">实时数据</option>
                                <option value="2051">分钟数据</option>
                                <option value="2061">小时数据</option>
                                <option value="2031">日均数据</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">参数：</label>

                        <div class="col-9">
                            <select id="codeDictList" class="form-control">
                                <option value="">请选择参数</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">类型：</label>

                        <div class="col-9">
                            <select id="sizeList" class="form-control" onchange="_alarmRuleList.fn.changeSize()">
                                <option value="1">超过预警</option>
                                <option value="0">低于预警</option>
                                <option value="2">等于预警</option>
                                <option value="3">固定预警</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">蓝线值：</label>

                        <div class="col-9">
                            <input type="number" id="alarmRuleBlueNum" maxlength="40" class="form-control"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">黄线值：</label>

                        <div class="col-9">
                            <input type="number" id="alarmRuleYellowNum" maxlength="40" class="form-control"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">红线值：</label>

                        <div class="col-9">
                            <input type="number" id="alarmRuleRedNum" maxlength="40" class="form-control"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">报警类别：</label>

                        <div class="col-9">
                            <select id="alarmTypeList" class="form-control">
                                <option value="1">间隔报警</option>
                                <option value="2">连续报警</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">报警时间：</label>

                        <div class="col-9">
                            <input type="number" id="alarmTime" maxlength="40" style="width: 110px;" class="form-control"/><span id="alarmTimeUnit" class="tableTopBtn">秒</span>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="_alarmRuleList.fn.saveInfo()">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<div class="modal inModal fade" id="batchAlarmRuleModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">批量添加告警规则</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <div class="form-inline">
                    <select id="batchAddTypeList" class="form-control" style="margin-bottom: 10px;width: 120px;text-align: center;" onchange="_alarmRuleList.fn.loadCodeDictByType()">
                        <option value="2011">实时数据</option>
                        <option value="2051">分钟数据</option>
                        <option value="2061">小时数据</option>
                        <option value="2031">日均数据</option>
                    </select>
                    <span style="margin-left: 10px;margin-top: -10px;color: red;font-weight: bolder;">提示：数值、排放标准为空则不添加，报警类别为间隔报警时报警时间可不填写；固定预警只需填写次数</span>
                </div>
                <table class="batch_table">
                    <tr id="batchAddTBody">
                        <th style="min-width: 150px;">参数</th>
                        <th>类型</th>
                        <th style="width: 70px;">蓝线值</th>
                        <th style="width: 70px;">黄线值</th>
                        <th style="width: 70px;">红线值</th>
                        <th>报警类别</th>
                        <th colspan="2">报警时间</th>
                    </tr>
                    <tr>
                    </tr>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="_alarmRuleList.fn.batchSaveInfo()">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<input type="hidden" id="hiddenDeviceCode" value="${device.code}"/>

<!-- page script -->
<script type="text/javascript">
    let _alarmRuleList = {
        v: {
            list: [],
            dTable: null,
            selectCodeDictName: '',
            uniqueSplitStr: '@@##%%',
            tempCodeDictList: []
        },
        fn: {
            init: function () {
                _alarmRuleList.fn.dataTableInit();

                _alarmRuleList.fn.getCodeDictList(0);
                _alarmRuleList.fn.getCodeDictList(1);
            },
            backList: function () {
                window.history.go(-1);
            },
            changeSize: function () {
                let dataType = Number($('#sizeList').val());

                if (dataType === 3) {
                    $('#alarmTimeUnit').html('次');
                    $('#alarmTimeUnit').parent().prev().html('报警次数');

                    $('#alarmRuleBlueNum').parent().parent().css('display', 'none');
                    $('#alarmRuleYellowNum').parent().parent().css('display', 'none');
                    $('#alarmRuleRedNum').parent().parent().css('display', 'none');
                    $('#codeDictUnit').parent().parent().css('display', 'none');
                    $('#alarmTypeList').parent().parent().css('display', 'none');
                } else {
                    let type = Number($('#typeList').val());

                    switch (type) {
                        case 2011:
                            $('#alarmTimeUnit').html('秒');
                            break;
                        case 2051:
                            $('#alarmTimeUnit').html('分');
                            break;
                        case 2061:
                            $('#alarmTimeUnit').html('时');
                            break;
                        case 2031:
                            $('#alarmTimeUnit').html('日');
                            break;
                    }

                    $('#alarmTimeUnit').parent().prev().html('报警时间：');
                    $('#alarmRuleBlueNum').parent().parent().css('display', '');
                    $('#alarmRuleYellowNum').parent().parent().css('display', '');
                    $('#alarmRuleRedNum').parent().parent().css('display', '');
                    $('#codeDictUnit').parent().parent().css('display', '');
                    $('#alarmTypeList').parent().parent().css('display', '');
                }
            },
            getCodeDictList: function (type) {
                let selectType;

                if (type === 0) {
                    // 搜索栏
                    selectType = $('#searchTypeList').val();
                    $('#searchCodeDictList').html('<option value="">全部参数</option>');
                } else if (type === 1) {
                    // 编辑栏
                    selectType = $('#typeList').val();
                    $('#codeDictList').html('<option value="">请选择参数</option>');

                    let dataType = $("#typeList").val();

                    if (dataType === '2011') {
                        $('#alarmTimeUnit').html('秒');
                    } else if (dataType === '2051') {
                        $('#alarmTimeUnit').html('分');
                    } else if (dataType === '2061') {
                        $('#alarmTimeUnit').html('时');
                    } else {
                        $('#alarmTimeUnit').html('日');
                    }
                }

                $.post('common/codeDictList', {
                    deviceCode: $('#hiddenDeviceCode').val(),
                    type: selectType
                }, function (result) {
                    if (result.length > 0) {
                        let content = '';
                        $.each(result, function (i, item) {
                            if (_alarmRuleList.v.selectCodeDictName === item.name) {
                                content += '<option value="' + item.name + '" selected>' + item.name + '</option>';
                            } else {
                                content += '<option value="' + item.name + '">' + item.name + '</option>';
                            }
                        });

                        if (type === 0) {
                            // 搜索栏
                            $('#searchCodeDictList').append(content);

                            $('#searchCodeDictList').comboSelect();
                        } else if (type === 1) {
                            // 编辑栏
                            $('#codeDictList').append(content);

                            $('#codeDictList').comboSelect();
                        }

                        changeSearchSelect();
                    } else {
                        info("暂无设备参数信息", "error");
                    }
                });
            },
            reloadData: function () {
                _alarmRuleList.v.list = [];

                $('.tableCheckBox').prop('checked', false);

                _alarmRuleList.v.dTable.DataTable().ajax.reload(null, false);
            },
            dataTableInit: function () {
                _alarmRuleList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许用户改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/alarmRule/list",
                        "type": "POST",
                        "data": {
                            "deviceCode": $('#hiddenDeviceCode').val()
                        }
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {"data": "id"},
                        {
                            "data": "type",
                            render: function (data) {
                                if (data === 2011) {
                                    return '实时数据';
                                } else if (data === 2051) {
                                    return '分钟数据';
                                } else if (data === 2061) {
                                    return '小时数据';
                                } else {
                                    return '日均数据';
                                }
                            }
                        },
                        {"data": "codeDictName"},
                        {
                            "data": "dataType",
                            render: function (data) {
                                if (Number(data) === 1) {
                                    return '大于';
                                } else if (Number(data) === 0) {
                                    return '小于';
                                } else if (Number(data) === 2) {
                                    return '等于';
                                } else {
                                    return '固定';
                                }
                            }
                        },
                        {"data": "blueNum"},
                        {"data": "yellowNum"},
                        {"data": "redNum"},
                        {"data": "codeDictUnit"},
                        {
                            "data": "alarmType",
                            render: function (data) {
                                return data === 1 ? '间隔报警' : '连续报警';
                            }
                        },
                        {"data": "alarmTime"},
                        {
                            "data": "lastTime",
                            render: function (data) {
                                return crtTimeFtt(data);
                            }
                        },
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='编辑' class='btn btn-primary btn-sm edit'><i class='fa fa-edit'></i></button>" +
                                "&nbsp;&nbsp;" +
                                "<button type='button' title='删除' class='btn btn-danger btn-sm delete'><i class='fa fa-remove'></i></button>",
                            "targets": -1
                        }
                    ],
                    /*展示数据格式化*/
                    "createdRow": function (row, data, index) {
                        _alarmRuleList.v.list.push(data);

                        $('td', row).eq(0).html("<input class='tableCheckBox' type='checkbox' name='checkboxId' value=" + data.id + ">");
                    },
                    /*回调函数*/
                    rowCallback: function (row, data) {
                        $('td', row).last().find(".edit").click(function () {
                            _alarmRuleList.fn.editInfo(data);
                        });

                        $('td', row).last().find(".delete").click(function () {
                            _alarmRuleList.fn.delInfo(data.id);
                        });
                    },
                    /*带参数*/
                    "fnServerParams": function (aoData) {
                        _alarmRuleList.v.list = [];

                        aoData.companyId = $('#searchCompanyList').val();
                        aoData.deviceCode = $('#hiddenDeviceCode').val();
                        aoData.type = $('#searchTypeList').val();
                        aoData.keyWord = $('#searchCodeDictList').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            saveInfo: function () {
                let sizeType = $('#sizeList').val();
                let codeDictName = $('#codeDictList').val();
                let codeDictUnit = $('#codeDictUnit').val();
                let blueNum = $('#alarmRuleBlueNum').val();
                let yellowNum = $('#alarmRuleYellowNum').val();
                let redNum = $('#alarmRuleRedNum').val();
                let alarmTime = $('#alarmTime').val();

                if (codeDictName === '') {
                    info('请选择参数', "error");
                    return;
                }

                if (sizeType !== '3') {
                    if (blueNum === '' || yellowNum === '' || redNum === '') {
                        info('请输入数值', "error");
                        return;
                    }

                    if (Number(blueNum) > 9999 || Number(blueNum) < 0 || Number(yellowNum) > 9999 || Number(yellowNum) < 0 || Number(redNum) > 9999 || Number(redNum) < 0) {
                        info('数值不合规', "error");
                        return;
                    }

                    if (codeDictUnit === '') {
                        info('请输入参数单位', "error");
                        return;
                    }

                    if (alarmTime === '') {
                        alarmTime = 0;
                    }
                } else {
                    if (alarmTime === '' || Number(alarmTime) <= 1) {
                        info('请输入有效的报警次数', "error");
                        return;
                    }

                    $('#alarmTypeList').val('2');
                }

                startLoading();

                $.post('backend/alarmRule/save', {
                    id: $('#alarmRuleId').val(),
                    dataType: sizeType,
                    deviceCode: $('#hiddenDeviceCode').val(),
                    type: $('#typeList').val(),
                    codeDictName: codeDictName,
                    codeDictUnit: codeDictUnit,
                    blueNum: blueNum,
                    yellowNum: yellowNum,
                    redNum: redNum,
                    alarmType: $('#alarmTypeList').val(),
                    alarmTime: alarmTime
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("信息已保存", "success");

                        $("#alarmRuleModal").modal("hide");

                        _alarmRuleList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            editInfo: function (data) {
                _alarmRuleList.v.selectCodeDictName = data.codeDictName;

                $('#alarmRuleId').val(data.id);

                $('#typeList').val(data.type);
                _alarmRuleList.fn.getCodeDictList(1);
                $('#sizeList').val(data.dataType);
                $('#alarmRuleBlueNum').val(data.blueNum);
                $('#alarmRuleYellowNum').val(data.yellowNum);
                $('#alarmRuleRedNum').val(data.redNum);
                $('#codeDictUnit').val(data.codeDictUnit);
                $('#alarmTypeList').val(data.alarmType);
                $('#alarmTime').val(data.alarmTime);

                $("#alarmRuleModal").modal("show");
            },
            addInfo: function () {
                _alarmRuleList.v.selectCodeDictName = '';

                $('#alarmRuleId').val('');

                $('#typeList').val(2011);
                _alarmRuleList.fn.getCodeDictList(1);
                $('#sizeList').val(1);
                $('#alarmRuleBlueNum').val('');
                $('#alarmRuleYellowNum').val('');
                $('#alarmRuleRedNum').val('');
                $('#codeDictUnit').val('');
                $('#alarmTypeList').val(1);
                $('#alarmTime').val('0');

                $("#alarmRuleModal").modal("show");
            },
            loadCodeDictByType: function () {
                let selectType = Number($('#batchAddTypeList').val());
                _alarmRuleList.v.tempCodeDictList = [];

                $.post('common/codeDictList', {
                    deviceCode: $('#hiddenDeviceCode').val(),
                    type: selectType
                }, function (result) {
                    if (result.length > 0) {
                        $('#batchAddTBody').siblings().remove();

                        $.each(result, function (i, item) {
                            if (!item.name.startsWith('参数'))
                                if (selectType === 2011) {
                                    if (item.code.indexOf('Rtd') !== -1) _alarmRuleList.v.tempCodeDictList.push(item.code + _alarmRuleList.v.uniqueSplitStr + item.name);
                                } else {
                                    if (item.code.indexOf('Avg') !== -1) _alarmRuleList.v.tempCodeDictList.push(item.code + _alarmRuleList.v.uniqueSplitStr + item.name);
                                }
                        });

                        let content = '';
                        let tempStr = '';

                        for (let i = 0; i < _alarmRuleList.v.tempCodeDictList.length; i++) {
                            tempStr = _alarmRuleList.v.tempCodeDictList[i].split(_alarmRuleList.v.uniqueSplitStr);
                            content = '<tr class="batch_Tr">';
                            content += '<td><span>' + tempStr[1] + '</span><span style="display: none;">' + tempStr[0] + '</span></td>';
                            content += '<td><select onchange="_alarmRuleList.fn.batchChangeType(this)"><option value="1">超过预警</option><option value="0">低于预警</option><option value="2">等于预警</option><option value="3">固定预警</option></select></td>';
                            content += '<td><input type="number" maxlength="40" /></td>';
                            content += '<td><input type="number" maxlength="40" /></td>';
                            content += '<td><input type="number" maxlength="40" /></td>';
                            content += '<td><select><option value="1">间隔报警</option><option value="2">连续报警</option></select></td>';
                            content += '<td style="width: 70px;border-right: none;"><input type="number" maxlength="10" onkeyup="_alarmRuleList.fn.changeInput(this)" /></td>';

                            switch (selectType) {
                                case 2011:
                                    content += '<td style="border-left: none;">秒</td>';
                                    break;
                                case 2051:
                                    content += '<td style="border-left: none;">分</td>';
                                    break;
                                case 2061:
                                    content += '<td style="border-left: none;">时</td>';
                                    break;
                                case 2031:
                                    content += '<td style="border-left: none;">日</td>';
                                    break;
                            }

                            content += '</tr>';

                            $('#batchAddTBody').after(content);
                        }

                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            changeInput: function (self) {
                $(self).val($(self).val().replace(/\D/g, ''));
            },
            batchAddInfo: function () {
                $('#batchAddTypeList').val(2011);

                _alarmRuleList.fn.loadCodeDictByType();

                $("#batchAlarmRuleModal").modal("show");
            },
            batchChangeType: function (self) {
                let selectType = Number($(self).val());
                let tempStr;

                if (selectType === 3) {
                    $(self).parent().next().children(0).val(''); // 蓝线值
                    $(self).parent().next().next().children(0).val(''); // 黄线值
                    $(self).parent().next().next().next().children(0).val(''); // 红线值
                    $(self).parent().next().next().next().next().children(0).val('2');
                    $(self).parent().next().next().next().next().next().next().html('次');

                    $(self).parent().next().children(0).prop('disabled', true);
                    $(self).parent().next().next().children(0).prop('disabled', true);
                    $(self).parent().next().next().next().children(0).prop('disabled', true);
                    $(self).parent().next().next().next().next().children(0).prop('disabled', true);
                    $(self).parent().next().next().next().next().next().next().prop('disabled', true);
                } else {
                    $(self).parent().next().next().next().next().children(0).val('1');

                    switch (Number($('#batchAddTypeList').val())) {
                        case 2011:
                            tempStr = '秒';
                            break;
                        case 2051:
                            tempStr = '分';
                            break;
                        case 2061:
                            tempStr = '时';
                            break;
                        case 2031:
                            tempStr = '日';
                            break;
                    }
                    $(self).parent().next().next().next().next().next().next().html(tempStr);

                    $(self).parent().next().children(0).prop('disabled', false);
                    $(self).parent().next().next().children(0).prop('disabled', false);
                    $(self).parent().next().next().next().children(0).prop('disabled', false);
                    $(self).parent().next().next().next().next().children(0).prop('disabled', false);
                    $(self).parent().next().next().next().next().next().next().prop('disabled', false);
                }
            },
            batchSaveInfo: function () {
                let codeStr = '';
                let sizeStr = '';
                let blueNumStr = '';
                let yellowNumStr = '';
                let redNumStr = '';
                let typeStr = '';
                let timeStr = '';

                let code = '';
                let size = '';
                let blueNum = '';
                let yellowNum = '';
                let redNum = '';
                let type = '';
                let time = '';

                $('.batch_Tr').each(function () {
                    code = $(this).children().eq(0).children().eq(0).html();
                    size = $(this).children().eq(1).children(':first').val();
                    blueNum = $(this).children().eq(2).children(':first').val();
                    yellowNum = $(this).children().eq(3).children(':first').val();
                    redNum = $(this).children().eq(4).children(':first').val();
                    type = $(this).children().eq(5).children(':first').val();
                    time = $(this).children().eq(6).children(':first').val();

                    if (time.trim().length === 0) time = 0;

                    if (Number(size) === 3) {
                        // 固定预警，此时只需要判断报警次数是否为空即可
                        if (time.trim().length > 0 && Number(time) > 0) {
                            codeStr += code + ",";
                            sizeStr += size + ",";
                            blueNumStr += blueNum + ",";
                            yellowNumStr += yellowNum + ",";
                            redNumStr += redNum + ",";
                            typeStr += type + ",";
                            timeStr += time + ",";
                        } else {
                            info('请输入正确的报警时间', "error");
                            return false;
                        }
                    } else {
                        // 其他预警，此时需要同时判断数值、排放标准、报警时间是否为空
                        if (Number(blueNum) > 0 || Number(yellowNum) > 0 || Number(redNum) > 0) {
                            codeStr += code + ",";
                            sizeStr += size + ",";
                            blueNumStr += blueNum + ",";
                            yellowNumStr += yellowNum + ",";
                            redNumStr += redNum + ",";
                            typeStr += type + ",";
                            timeStr += time + ",";
                        } else {
                            info('请输入正确的报警时间', "error");
                            return false;
                        }
                    }
                });

                startLoading();

                $.post('backend/alarmRule/batchAdd', {
                    deviceCode: $('#hiddenDeviceCode').val(),
                    type: $('#batchAddTypeList').val(),
                    codeStr: codeStr,
                    sizeStr: sizeStr,
                    blueNumStr: blueNumStr,
                    yellowNumStr: yellowNumStr,
                    redNumStr: redNumStr,
                    typeStr: typeStr,
                    timeStr: timeStr
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("批量添加告警规则成功！！", "success");

                        $("#batchAlarmRuleModal").modal("hide");

                        setTimeout(function () {
                            _alarmRuleList.fn.reloadData();
                        }, 2000);
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            delInfo: function (id) {
                swal({
                    title: "您确定要删除这条记录吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的，删除！",
                    cancelButtonText: "不，取消",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        startLoading();

                        $.post('backend/alarmRule/del', {
                            id: id
                        }, function (result) {
                            endLoading();

                            if (result.code === 0) {
                                swal("删除成功！", "您已经删除了这条记录。", "success");

                                _alarmRuleList.fn.reloadData();
                            } else {
                                info(result.msg, "error");
                            }
                        });
                    } else {
                        swal("已取消", "您取消了删除操作！", "error");
                    }
                })
            },
            subDelInfo: function (id) {
                swal({
                    title: "您确定要删除选中的所有告警规则吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的，删除！",
                    cancelButtonText: "不，取消",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        startLoading();

                        $.post('backend/alarmRule/batchDel', {
                            ids: id
                        }, function (result) {
                            endLoading();

                            if (result.code === 0) {
                                swal("删除成功！", "您已经删除了这些告警规则。", "success");

                                setTimeout(function () {
                                    _alarmRuleList.fn.reloadData();
                                }, 1000);
                            } else {
                                info(result.msg, "error");
                            }
                        });
                    } else {
                        swal("已取消", "您取消了删除操作！", "error");
                    }
                })
            },
            batchDelModal: function () {
                let ids = "";

                if ($('input:checkbox[name=checkboxId]:checked').length === 0) {
                    info('请先选择至少一条告警规则', 'error');
                } else {
                    $('input:checkbox[name=checkboxId]:checked').each(function (i) {
                        if (0 === i) {
                            ids = $(this).val();
                        } else {
                            ids += ("," + $(this).val());
                        }
                    });

                    _alarmRuleList.fn.subDelInfo(ids);
                }
            }
        }
    };

    $(document).ready(function () {
        _alarmRuleList.fn.init();

        window.onload = function () {
            checkSelectMenu("deviceLi");

            loadMenuColor('deviceLi');
        }
    });
</script>
</body>
</html>